<!--途径-->
<template>
  <div>
    <el-select v-model="administration" :clearable="clearable" :disabled="props.disabled" :placeholder="placeholder"
               :filter-method="filterMethod" filterable
               style="width: 100%;" @change="producerChange" @keyup.enter.native="changeFocusAdmin">
      <el-option v-for="item in administrationFilterList" :key="item.dictValue" :label="item.dictLabel"
                 :value="item.dictValue">
      </el-option>
    </el-select>
  </div>
</template>

<script lang="ts" setup>
import {useDictStoreHook} from "@/store/modules/dict";
import {firstLetterResult} from "@/utils/pinyin";

const dictStore = useDictStoreHook();

// 途径list
const administrationList = ref<any>(dictStore.administrationList)

const administrationFilterList = ref<any>(administrationList.value)
const emits = defineEmits(["administrationInfoChange", "changeFocusAdmin"])
const props = defineProps({
  placeholder: {
    type: String,
    default: "请选择"
  },
  value: {
    type: String,
    default: ''
  },
  // 机构id
  orgId: {
    type: String,
    default: ''
  },
  disabled: {
    type: Boolean,
    default: false
  },
  clearable: {
    type: Boolean,
    default: true
  }
})
const administration = ref(props.value)
// watch(() => props.orgId, (_val, _old) => {
//     getAdministrationList()
// }, {deep: true, immediate: true})
watch(() => props.value, (val, _old) => {
  administration.value = val
})

// // 获取本机构途径list
// function getAdministrationList() {
//     const param = {delFlag: '0', orgId: props.orgId}
//     // if (!administrationList.value) {
//         listAdministrationDict(param).then(res => {
//             administrationList.value = res.data
//         })
//     // }
// }

function producerChange(value: string) {
  let administrationInfo = {}
  let index = administrationList.value.findIndex((item: any) => {
    return item.dictValue === value
  })
  if (index > -1) {
    administrationInfo = administrationList.value[index]
  }
  emits("administrationInfoChange", administrationInfo)
}

function changeFocusAdmin() {
  emits("changeFocusAdmin")
}

/**
 * 筛选选项
 * @param searchContent
 */
function filterMethod(searchContent: string) {
  if (searchContent) {
    administrationFilterList.value = administrationList.value.filter((item: any) => {
      return firstLetterResult(item.dictLabel).toLowerCase().includes(searchContent) ||
        item.dictLabel.includes(searchContent)
    })
  } else {
    administrationFilterList.value = administrationList.value
  }
}
</script>
<script lang="ts">
export default {
  name: "AdministrationDict"
}
</script>

<style scoped></style>
